1 : <?php
2 :
3 : /*--------------------------------------------------------------------------+
4 : This file is part of eStudy
5 : externaltools/classes/class.wordpressinfo.inc.php
6 : - Modulgruppe: ExternalTools
7 : - Beschreibung: In dieser Datei ist die Klasse WordPressInfo implementiert.
8 : - Version: 1.1 02-07-07
9 : - Autor(en): Jörg Rieger
10 : +---------------------------------------------------------------------------+
11 : This program is free software; you can redistribute it and/or
12 : modify it under the terms of the GNU General Public License
13 : as published by the Free Software Foundation; either version 2
14 : of the License, or any later version.
15 : +---------------------------------------------------------------------------+
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 : +--------------------------------------------------------------------------*/
24 :
25 1 : if (!defined('PATH_TO_ROOT'))
26 1 : define("PATH_TO_ROOT", "../../");
27 :
28 : /**
29 : * Die Basisklasse
30 : */
31 1 : require_once ("class.toolinfo.inc.php");
32 1 : require_once ("class.wordpresscategorizer.inc.php");
33 :
34 : /**
35 : * Klasse zum Anlegen eines neuen Adresseintrages im Profil
36 : */
37 1 : require_once ("class.editcontact.inc.php");
38 :
39 : /**
40 : * Passwort Klasse für die Hash Erzeugung
41 : */
42 1 : require_once ("class.passwords.inc.php");
43 :
44 :
45 1 : define("WORDPRESS_USER_TABLE", "wp_users");
46 1 : define("WORDPRESS_META_USER_TABLE", "wp_usermeta");
47 1 : define("WORDPRESS_BLOGS_TABLE", "wp_blogs");
48 1 : define("WORDPRESS_SITE_TABLE", "wp_site");
49 :
50 1 : define("WORDPRESS_SQL_DUMP", PATH_TO_ROOT . "externaltools/wordpress_template.sql");
51 1 : define("WORDPRESS_CAT_NUM", 1);
52 1 : define("WORDPRESS_MAIN_CAT", 3);
53 :
54 : /**
55 : * ToolInfo Klasse für WordPress
56 : *
57 : * @package eStudy.ExternalTools
58 : * @version 1.0, 01/22/08
59 : * @author Jörg Rieger <joerg.rieger@mni.fh-giessen.de>
60 : */
61 1 : class WordPressInfo extends ToolInfo {
62 :
63 : protected $wpSql = '';
64 : protected $blogID = null;
65 :
66 : /**
67 : * Erzeugt ein neues WordPressInfo Objekt.
68 : *
69 : * @param integer $id - die Tool ID
70 : * @param string $image - das Icon, das angezeigt werden soll.
71 : */
72 : public function WordPressInfo($id, $image = null) {
73 0 : parent :: ToolInfo($image);
74 0 : $this->toolName = 'WordPress';
75 0 : $this->toolID = $id;
76 0 : }
77 :
78 : public function logon( $userID ) {
79 0 : $ret = parent :: logon( $userID );
80 0 : $this->synchronizeCategories();
81 0 : return $ret;
82 : }
83 :
84 : public function getUserCategoryIDs() {
85 0 : if ( $this->blogID != null ) {
86 0 : $blogID = Data::toMysql( $this->blogID, false );
87 0 : $sql = "SELECT SUBSTRING(slug, 5) FROM wp_" . $this->blogID . "_terms WHERE term_group=" . WORDPRESS_CAT_NUM;
88 0 : return (array) $this->toolDB->get_col( $sql );
89 : } else {
90 0 : return null;
91 : }
92 : }
93 :
94 : /**
95 : * Synchronisiert die Wordpress-Kategorien mit
96 : * den Kursen des Users und erstellt gegebenenfalls neue Kategorien
97 : */
98 : public function synchronizeCategories() {
99 0 : if ( $this->getBlogID() != null ) {
100 0 : global $db;
101 :
102 0 : $categoryIDs = $this->getUserCategoryIDs();
103 0 : $wpc = new WordPressCategorizer( $db, $_SESSION['userid'] );
104 0 : $courses = $wpc->getUserCoursesAsCategories();
105 0 : foreach ( $courses as $courseID => $courseName ) {
106 0 : if ( !in_array( $courseID, $categoryIDs )) {
107 0 : $this->toolDB->query( "INSERT INTO wp_". $this->blogID ."_terms (name, slug, term_group) VALUES ('$courseName', 'kurs$courseID', '" .WORDPRESS_CAT_NUM. "')" );
108 0 : $termID = $this->toolDB->insert_id;
109 0 : $this->toolDB->query( "INSERT INTO wp_". $this->blogID ."_term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ('$termID', 'category', '', '" .WORDPRESS_MAIN_CAT. "', 0 )");
110 0 : }
111 0 : }
112 0 : }
113 0 : }
114 :
115 : /**
116 : * Aktualisiert die Jabber Benutzerdaten, nachdem der
117 : * eStudy-Benutzer innerhalb von eStudy seine Logindaten geändert hat.
118 : *
119 : * @access public
120 : * @param String $oldLogin - Der alte Loginname des Benutzers.
121 : * @param String $newLogin - Der neue Loginname des Benutzers.
122 : * @param String $password - Das Passwort des Benutzers.
123 : * @return bool - <i>true</i> wenn alles geklappt hat.
124 : */
125 : public function updateLogin($oldLogin, $newLogin, $password) {
126 0 : global $settings;
127 0 : $bool = false;
128 :
129 0 : if ( $this->isExistingUser($newLogin) )
130 0 : $bool = $this->updateUser($oldLogin, $newLogin, $password);
131 : else
132 0 : $bool = $this->insertNewUser($newLogin, $password);
133 :
134 0 : return $bool;
135 : }
136 :
137 : /**
138 : * Erstellt einen Nutzer in WordPress
139 : *
140 : * @access public
141 : * @param String $login - Loginname des Benutzers
142 : * @param String $password - Das Passwort des Benutzers.
143 : * @return bool - <i>true</i> wenn alles geklappt hat.
144 : */
145 : public function insertNewUser( $login='', $password='' ) {
146 0 : $login = ($login=='') ? $this->userLogonData['login'] : $login;
147 0 : $password = ($password=='') ? $this->userLogonData['password'] : $password;
148 :
149 0 : $userId = $this->insertUser($login, $password); // Neue auto_increment ID des Nutzers, wird für die weiteren Daten benötigt
150 :
151 0 : $siteData = $this->getSiteData(); // Daten der Site Einträge
152 0 : $this->blogID = $this->insertNewBlog($siteData['id'], $siteData['domain'], $siteData['path'] . $login . '/'); // neue Blog Id
153 :
154 : // SQL Template einlesen
155 0 : $this->getWordPressTemplate();
156 :
157 : // Im SQL Template ersetzen
158 0 : $this->replaceTag('BLOG_ID', $this->blogID);
159 0 : $this->replaceTag('USER_ID', $userId);
160 0 : $this->replaceTag('FULL_URL', 'http://' . $siteData['domain'] . $siteData['path'] . $login . '/');
161 0 : $this->replaceTag('FIRSTNAME', Data::toMysql($this->userData['Vorname']));
162 0 : $this->replaceTag('LASTNAME', Data::toMysql($this->userData['Nachname']));
163 0 : $this->replaceTag('USER_EMAIL', Data::toMysql($this->userData['Email']));
164 0 : $this->replaceTag('WP_URL_WT', substr('http://' . $siteData['domain'] . $siteData['path'], 0, strlen('http://' . $siteData['domain'] . $siteData['path']) - 1));
165 0 : $this->replaceTag('WP_URL_T', 'http://' . $siteData['domain'] . $siteData['path']);
166 :
167 : // Anhand des SQL Templates Blog erzeugen
168 0 : $this->createNewBlog();
169 :
170 : // Wordpress-Kategorien aus den eStudy-Kursen des Users erstellen
171 0 : $this->synchronizeCategories();
172 :
173 0 : return true;
174 : }
175 :
176 : /**
177 : * Gibt die BlogID des angegebenen Users zurueck
178 : * Ist kein Blog vorhanden, wird null zurueckgegeben
179 : *
180 : * @param int $userID des eStudy-Nutzers
181 : */
182 : public function getBlogID() {
183 0 : if ( $this->blogID == null ) {
184 0 : $login = Data::toMysql($this->userLogonData['login'], false);
185 0 : $sql = "SELECT blog_id FROM wp_blogs WHERE INSTR(path, '$login')>0 ";
186 0 : $this->blogID = $this->toolDB->get_var( $sql );
187 0 : }
188 0 : return $this->blogID;
189 : }
190 :
191 : /**
192 : * Aktualisiert die Daten eines WordPress Nutzers
193 : *
194 : * @access public
195 : * @param String $oldLogin - Loginname des Benutzers
196 : * @param String $newLogin - Loginname des Benutzers
197 : * @param String $password - Das Passwort des Benutzers.
198 : * @return bool - <i>true</i> wenn alles geklappt hat.
199 : */
200 : public function updateUser( $oldLogin, $newLogin, $password ) {
201 : // Passwort Hash, WordPress verwendet plain MD5
202 0 : $pwHash = Passwords::generateHash($password, null, 'MD5');
203 0 : $c_oldLogin = Data::toMysql($oldLogin);
204 0 : $c_newLogin = Data::toMysql($newLogin);
205 :
206 0 : $sql = "UPDATE " . WORDPRESS_USER_TABLE . " SET user_login = '" . $c_newLogin . "', user_pass = '" . $pwHash . "' WHERE user_login = '" . $c_oldLogin . "'";
207 :
208 0 : if ( $this->toolDB->query($sql) )
209 0 : return true;
210 : else
211 0 : return false;
212 : }
213 :
214 : /**
215 : * Erstellt einen Nutzer in WordPress
216 : *
217 : * @access public
218 : * @param String $login - Loginname des Benutzers
219 : * @param String $password - Das Passwort des Benutzers.
220 : * @return bool - <i>true</i> wenn alles geklappt hat.
221 : */
222 : public function insertUser( $login, $password ) {
223 : // Passwort Hash, WordPress verwendet plain MD5
224 0 : $pwHash = Passwords::generateHash($password, null, 'MD5');
225 0 : $c_login = Data::toMysql($login);
226 :
227 0 : $sql = "INSERT INTO " . WORDPRESS_USER_TABLE . "(user_login,
228 : user_pass,
229 : user_nicename,
230 : user_email,
231 : user_url,
232 : user_registered,
233 : user_status,
234 : display_name,
235 : spam,
236 : deleted
237 : )
238 0 : VALUES ('$c_login',
239 0 : '$pwHash',
240 0 : '$c_login',
241 0 : '" . Data::toMysql($this->userData['Email']) . "',
242 : '',
243 0 : '" . date("Y-m-d H:i:s") . "',
244 : '0',
245 0 : '" . $this->userData['Vorname'] . " " . $this->userData['Nachname'] . "',
246 : '0',
247 : '0'
248 0 : )";
249 0 : $this->toolDB->query($sql);
250 0 : $userId = $this->toolDB->insert_id; // Neue auto_increment ID des Nutzers, wird für die weiteren Daten benötigt
251 :
252 0 : return $userId;
253 : }
254 :
255 : /**
256 : * Prüft ob der Nutzer bereits in Wordpress eingetragen ist
257 : *
258 : * @access public
259 : * @param String $login - Loginname des Benutzers
260 : * @return boolean
261 : */
262 : public function isExistingUser( $login ) {
263 0 : $sql = "SELECT ID FROM " . WORDPRESS_USER_TABLE . " WHERE user_login = '" . Data::toMysql($login) . "' LIMIT 1";
264 :
265 0 : if ( $this->toolDB->get_row($sql) != null )
266 0 : return true;
267 : else
268 0 : return false;
269 : }
270 :
271 : /**
272 : * Gibt die Daten der Site Einträge zurück
273 : *
274 : * @access public
275 : * @return array
276 : */
277 : public function getSiteData() {
278 0 : $sql = "SELECT * FROM " . WORDPRESS_SITE_TABLE . " LIMIT 1";
279 :
280 0 : return $this->toolDB->get_row($sql, ARRAY_A);
281 : }
282 :
283 : /**
284 : * Fügt ein neues Blog ein
285 : *
286 : * @access public
287 : * @param int $siteId
288 : * @param string $domain
289 : * @param string $path
290 : * @return array
291 : */
292 : public function insertNewBlog( $siteId, $domain, $path) {
293 0 : $sql = "INSERT INTO " . WORDPRESS_BLOGS_TABLE . " (blog_id,
294 : site_id,
295 : domain, path,
296 : registered,
297 : last_updated,
298 : public,
299 : archived,
300 : mature,
301 : spam,
302 : deleted,
303 : lang_id )
304 : VALUES (NULL,
305 0 : '$siteId',
306 0 : '$domain',
307 0 : '$path',
308 : NOW(),
309 : NOW(),
310 : '0',
311 : '0',
312 : '0',
313 : '0',
314 : '0',
315 : '0'
316 0 : )";
317 :
318 0 : $this->toolDB->query($sql);
319 :
320 0 : return $this->toolDB->insert_id;
321 : }
322 :
323 : /**
324 : * Fügt ein neues Blog ein
325 : *
326 : * @access public
327 : * @return string
328 : */
329 : public function getWordPressTemplate() {
330 : try {
331 0 : $this->wpSql = file(realpath(WORDPRESS_SQL_DUMP));
332 : }
333 0 : catch(Exception $e) {
334 :
335 : }
336 0 : }
337 :
338 : /**
339 : * Ersetz Tag
340 : *
341 : * @access public
342 : * @return string
343 : */
344 : public function replaceTag( $tag, $string ) {
345 0 : for($i = 0; $i < count($this->wpSql); $i++) {
346 0 : $this->wpSql[$i] = str_replace("-$tag-", $string, $this->wpSql[$i]);
347 0 : }
348 0 : }
349 :
350 : /**
351 : * Einzelne Sql Statements parsen
352 : * Beachtet Multiple-line SQL Statements
353 : *
354 : * (TODO: Regex verwenden)
355 : *
356 : * @access public
357 : * @return array
358 : */
359 : public function splitSql() {
360 0 : $j = 0;
361 0 : $tmp = '';
362 0 : $sql = array();
363 :
364 0 : for($i = 0; $i < count($this->wpSql); $i++) {
365 0 : $this->wpSql[$i] = trim($this->wpSql[$i]);
366 :
367 0 : if ( strrpos($this->wpSql[$i], ';') == strlen($this->wpSql[$i]) - 1 ) {
368 0 : $sql[$j] = $tmp . $this->wpSql[$i];
369 :
370 0 : $tmp = '';
371 0 : $j++;
372 0 : }
373 : else
374 0 : $tmp .= $this->wpSql[$i];
375 0 : }
376 :
377 0 : return $sql;
378 : }
379 :
380 :
381 : /**
382 : * Neues Blog erzeugen
383 : *
384 : * @access public
385 : * @return string
386 : */
387 : public function createNewBlog() {
388 0 : $sqlArray = $this->splitSql();
389 :
390 : // Schickt die Abfragen aus der Datei wordpress_template.sql an die DB
391 : // da von PHP keine multiple sql queries unterstützt werden
392 0 : foreach($sqlArray as $sql) {
393 0 : $this->toolDB->query($sql);
394 0 : }
395 0 : }
396 :
397 : public function checkForUser() {
398 0 : return $this->isExistingUser( $this->userLogonData['login'] );
399 : }
400 :
401 :
402 : }
403 : ?>
|